package com.sync.utils.agent.consumer;

import com.sync.utils.agent.pojo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.util.concurrent.TimeoutException;

/**
 * TODO
 *
 * @Author YWF
 * @Date 2025/5/10 22:54
 */
public class ConsumerHandler implements Consumer {

    protected final static Logger logger = LoggerFactory.getLogger(ConsumerHandler.class);

    @Resource
    private AgentClient agentClient;

    protected ConsumerDto handler(ServerReq serverReq){
        ConsumerDto consumerDto = new ConsumerDto();
        try {
            ServerRes serverRes = agentClient.excute(serverReq);
            consumerDto.setServerReq(serverReq);
            consumerDto.setServerRes(serverRes);
            consumerDto.setSystem(SourceSystem.WE_CHAT);
            consumerDto.setStatus(status(serverRes));
        } catch (TimeoutException e) {
            throw new RuntimeException(e);
        }
        return consumerDto;
    }

    @Override
    public ConsumerDto communicate(ServerReq request) throws TimeoutException {
        return handler(request);
    }


    /**
     * 这个方法需要暴漏到具体的客户端
     * @param serverRes
     * @return
     */
    protected ResponseStatus status(ServerRes serverRes) {
           switch (serverRes.getReturnCode()) {
            case "0":
                return ResponseStatus.SUCCESS;
            case "1":
                return ResponseStatus.FAILD;
            case "3":
                return ResponseStatus.EXCEPTION;
            default:
                return ResponseStatus.UNKOWN;
        }
    }


}
